package com.atguigu.springcloud.alibaba.service.impl;

import com.atguigu.springcloud.alibaba.dao.OrderDao;
import com.atguigu.springcloud.alibaba.domain.Order;
import com.atguigu.springcloud.alibaba.service.AccountService;
import com.atguigu.springcloud.alibaba.service.OrderService;
import com.atguigu.springcloud.alibaba.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;

    @Override
    public Order getById(Integer id) {
        return orderDao.getById(id);
    }

    @Autowired
    private StorageService storageService;

    @Autowired
    private AccountService accountService;

    @Override
    @GlobalTransactional(name = "fsp_tx_group",rollbackFor = Exception.class)
    public void createOrder(Order order) {
        log.info("------> 新建订单");
        Long id = orderDao.createOrder(order);
        order.setId(id);
        log.info("------> 订单微服务调用库存微服务，扣减库存开始");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("------> 订单微服务调用库存微服务，扣减库存完成");
        log.info("------> 订单微服务调用账户微服务，扣减余额开始");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("------> 订单微服务调用账户微服务，扣减余额完成");
        log.info("------> 修改订单的状态");
        orderDao.updateStatus(order.getId(), 0);
        log.info("------> 修改订单的状态完成");
        log.info("------> 订单完成");

    }
}
